package com.gzw.shop.activity;

import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TabHost;
import android.widget.TextView;

import com.gzw.shop.R;
import com.gzw.shop.bean.Tab;
import com.gzw.shop.fragment.CartFragment;
import com.gzw.shop.fragment.CategoryFragment;
import com.gzw.shop.fragment.HomeFragment;
import com.gzw.shop.fragment.HotFragment;
import com.gzw.shop.fragment.MineFragment;
import com.gzw.shop.widget.FragmentTabHost;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends BaseActivity implements View.OnClickListener {

    private FragmentTabHost mTabHost;
    private List<Tab> mTabs;

    private LayoutInflater mInflater;

    private HomeFragment homeFragment;
    private HotFragment hotFragment;
    private CategoryFragment categoryFragment;
    private CartFragment mCartFragment;

    @Override
    public int getLayoutId() {
        return R.layout.activity_main;
    }

    @Override
    public void initView() {
        initTab();
    }

    @Override
    public void initData() {

    }

    private void initTab() {

        mTabs = new ArrayList<>();

        Tab home = new Tab(R.string.home, R.drawable.selector_icon_home, HomeFragment.class);
        Tab hot = new Tab(R.string.hot, R.drawable.selector_icon_hot, HotFragment.class);
        Tab category = new Tab(R.string.category, R.drawable.selector_icon_category, CategoryFragment.class);
        final Tab cart = new Tab(R.string.cart, R.drawable.selector_icon_cart, CartFragment.class);
        Tab mine = new Tab(R.string.mine, R.drawable.selector_icon_mine, MineFragment.class);

        mTabs.add(home);
        mTabs.add(hot);
        mTabs.add(category);
        mTabs.add(cart);
        mTabs.add(mine);

        mInflater = LayoutInflater.from(this);
        mTabHost = (FragmentTabHost) findViewById(android.R.id.tabhost);
        mTabHost.setup(this, getSupportFragmentManager(), R.id.realtabcontent);

        for (int i = 0; i < mTabs.size(); i++) {

            Tab tab = mTabs.get(i);

            TabHost.TabSpec tabspec = mTabHost.newTabSpec(getString(tab.getTitle()));

            View view = mInflater.inflate(R.layout.tab_indicator, null);
            ImageView iv = (ImageView) view.findViewById(R.id.icon_tab);
            TextView tv = (TextView) view.findViewById(R.id.txt_indicator);
            iv.setImageResource(tab.getIcon());
            tv.setText(tab.getTitle());
            tabspec.setIndicator(view);
            mTabHost.addTab(tabspec, tab.getFragment(), null);

            mTabHost.getTabWidget().setDividerDrawable(null);

            //设置每个Tab的点击事件
            View view1 = mTabHost.getTabWidget().getChildAt(i);
            view1.setId(i);
            view1.setOnClickListener(this);
        }

        //取消分割线
        mTabHost.getTabWidget().setShowDividers(LinearLayout.SHOW_DIVIDER_NONE);

        //默认设置第一页
        mTabHost.setCurrentTab(0);
    }


    //刷新购物车数据
    private void refreshData(int tag) {

        switch (tag) {

            case 0:
                if (homeFragment == null) {
                    Fragment fragment = getSupportFragmentManager().findFragmentByTag(getString(R.string.home));
                    if (fragment != null) {
                        homeFragment = (HomeFragment) fragment;
//                        homeFragment.showData();
                    }
                } else {
                    homeFragment.showData();
                }
                break;
            case 1:
                if (hotFragment == null) {
                    Fragment fragment = getSupportFragmentManager().findFragmentByTag(getString(R.string.hot));
                    if (fragment != null) {
                        hotFragment = (HotFragment) fragment;
//                        hotFragment.showData();
                    }
                } else {
                    hotFragment.showData();
                }
                break;
            case 2:
                if (categoryFragment == null) {
                    Fragment fragment = getSupportFragmentManager().findFragmentByTag(getString(R.string.category));
                    if (fragment != null) {
                        categoryFragment = (CategoryFragment) fragment;
//                        categoryFragment.refreshView();
                    }
                } else {
                    categoryFragment.refreshView();
                }
                break;
            case 3:
                if (mCartFragment == null) {
                    Fragment fragment = getSupportFragmentManager().findFragmentByTag(getString(R.string.cart));
                    if (fragment != null) {
                        mCartFragment = (CartFragment) fragment;
//                        mCartFragment.showData();
                    }
                } else {
                    mCartFragment.showData();
                }
                break;
        }
    }

    @Override
    public void onClick(View v) {
        mTabHost.setCurrentTab(v.getId());
        refreshData(v.getId());
    }
}
